package com.jason.game.online.widget

import android.content.Context
import android.widget.LinearLayout
import android.widget.TextView
import android.text.TextUtils
import android.text.SpannableString
import android.util.AttributeSet
import android.view.Gravity
import androidx.core.content.ContextCompat.getColor
import androidx.core.content.ContextCompat.getDrawable
import com.cby.library.common.util.ScreenUtils
import com.jason.game.online.R

/**
 * 游戏名称-礼包
 */
class GameTitleItem : LinearLayout {
    private lateinit var mTextView: TextView

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        orientation = HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL
        mTextView = TextView(context)
        mTextView.textSize = 17f
        mTextView.isSingleLine = true
        mTextView.ellipsize = TextUtils.TruncateAt.END
        mTextView.setTextColor(getColor(context, R.color.font_secondary_black))
        addView(mTextView)
    }

    fun setData(name: String?, gift: Int) {
        setData(name, gift == 1)
    }

    /**
     * 设置分类数据
     * @param name
     * @param hasGift
     */
    fun setData(name: String?, hasGift: Boolean) {
        mTextView!!.text = name
        //判断是否包含礼包，有礼包则添加icon
        if (hasGift) {
            mTextView!!.compoundDrawablePadding = ScreenUtils.dip2px(context, 4f)
            mTextView!!.setCompoundDrawables(
                null, null,
                getDrawable(context, R.drawable.icon_gift), null
            )
        } else {
            mTextView!!.compoundDrawablePadding = 0
            mTextView!!.setCompoundDrawables(null, null, null, null)
        }
    }

    /**
     * 设置分类数据
     * @param name
     * @param hasGift
     */
    fun setData(name: SpannableString?, hasGift: Int) {
        mTextView!!.text = name
        //判断是否包含礼包，有礼包则添加icon
        if (hasGift == 1) {
            mTextView!!.compoundDrawablePadding = ScreenUtils.dip2px(context, 4f)
            mTextView!!.setCompoundDrawables(
                null, null, getDrawable(context, R.drawable.icon_gift), null
            )
        } else {
            mTextView!!.compoundDrawablePadding = 0
            mTextView!!.setCompoundDrawables(null, null, null, null)
        }
    }

    /**
     * 当文本长度过长时，在需要的时候设置最大宽度
     * @param maxWidth
     */
    fun setTitleMaxWidth(maxWidth: Int) {
        mTextView!!.maxWidth = maxWidth
    }
}